import { mapState, mapMutations } from 'vuex'
import { Toast } from 'mint-ui';
import lodash from 'lodash'
import { reverseDate } from '../../config/util'
import Loading from "../../components/loading/loading.vue"
export default {
	name: 'interBack',
	data() {
		return {
			loading:true,
			asTime:false,//按照时间排序
			asPrice:true,//按照价格排序
			flightData: [],//页面展示
			storeData:null,
			
			orderByText:{
				time:"时间",
				price:"价格"
			},
			orderByActive:{
				time:false,
				price:false
			}
		}
	},
	computed: {
		...mapState([
			'city', 'Date', 'cabin', 'isBack', 'adultCount','isInland','flight','interRT'
		]),
		
	},
	methods: {
		...mapMutations([
			'RECORD_FLIGHT','RECORD_INTERRT'
		]),
		selectTheFlight(flight,i) {	
			var that = this;
			this.RECORD_INTERRT({type:'back'});
			this.RECORD_FLIGHT({type:'inter',value:getData()});
			this.$router.push({ path: '/cabin' });
			//将选中的展示数据映射到存储数据
			function getData(){
				for(var j=0;j<that.storeData.length;j++){
					if(flight.index == that.storeData[j].index){
						return that.storeData[j]
					}
				}
			}
		},
		reChoose(){
			this.$router.push({ path: '/flight' });
		},
		
		successHandle(data) {
			if(data.status == 1001) {
				this.flightData = data.data;				
			} else {
				Toast(data.message);
			}
		},
		errorHandle(res) {
			Toast('请求失败了哦');
		},
		flightDuration(value){
			var duration = 0;
			for(var i=0;i<value.length;i++){
				duration+=value[i].flightDuration;
			}
			return duration;
		},
		//跨天
		acrossDay(flight){
			let rangeSegmentCount = flight.rangeSegmentCount;
			let legs = rangeSegmentCount.split("-")[1];
			let fromDate = flight.segments[0].fromDate.substring(0,10);
			let toDate = flight.segments[legs-1].toDate.substring(0,10);
			let count = parseInt((reverseDate(toDate) - reverseDate(fromDate))/86400000);
			return count;
		},
		imgName(flight){
			var name = (flight.segments[0].flightNo).slice(0,2);			
			return name+".PNG";
		},
		tranformTime(flight){
			let rangeSegmentCount = flight.rangeSegmentCount;
			let legs = rangeSegmentCount.split("-")[1];
			if(legs>1){
				var toDate = flight.segments[0].toDate;
				var fromDate = flight.segments[1].fromDate;
				var min = (reverseDate(fromDate) - reverseDate(toDate)) / 60000;
				return min;
			}		
		},
		screen(){
			alert("1")
		},
		filterAsTime(){
			this.asPrice = false;
			this.orderByActive = {time:true,price:false};
			if(this.asTime){	
				this.orderByText = {time:'从早到晚',price:'价格'};
				this.flightData = _.orderBy(this.flightData, 'segments[0].fromDate'); //由低到高
			}else{
				this.orderByText = {time:'从晚到早',price:'价格'};
				this.flightData = _.orderBy(this.flightData, 'segments[0].fromDate').reverse();//由高到低
			}				
			this.asTime = !this.asTime;
		},
		filterAsPrice(){
			this.asTime = true;
			this.orderByActive = {time:false,price:true};
			if(this.asPrice){
				this.orderByText = {time:'时间',price:'从高到低'};
				this.flightData = _.orderBy(this.flightData, 'minAdultFacePrice').reverse(); //由低到高		
			}else{
				this.orderByText = {time:'时间',price:'从低到高'};
				this.flightData = _.orderBy(this.flightData, 'minAdultFacePrice'); //由高到低
			}		
			this.asPrice = !this.asPrice;		
		},
		//国际往返回程数据整合
		getBackData(){
			var flight = [];
			//获取选中的航班的去程的航班号
			var flightNoArr = [];
			var rangeSegmentCount = this.flight.inter.rangeSegmentCount.split(",")[0].split("-")[1];
			for(var i=0;i<rangeSegmentCount;i++){
				flightNoArr.push(this.flight.inter.segments[i].flightNo);
			}
			//筛选相同的航班
			for(var i=0;i<this.interRT.data.length;i++){
				var rangeSegmentCount_ = this.interRT.data[i].rangeSegmentCount.split(",")[0].split("-")[1];
				var flightNoItem = [];
				for(var j=0;j<rangeSegmentCount_;j++){
					var flightNo = this.interRT.data[i].segments[j].flightNo;
					flightNoItem.push(flightNo);
				}
				if(JSON.stringify(flightNoArr)==JSON.stringify(flightNoItem)){
					flight.push(this.interRT.data[i]);
				}
			}
			//存储原始数据
			this.storeData  = JSON.parse(JSON.stringify(flight));
			//显示数据
			this.flightData  = JSON.parse(JSON.stringify(flight));
			//过滤回程
			for(var i=0;i<this.flightData.length;i++){
				var rangeSegmentCount_ = this.flightData[i].rangeSegmentCount.split(",")[0].split("-")[1];
				var leg = this.flightData[i].rangeSegmentCount.split(",")[1];
				this.flightData[i].segments.splice(0,rangeSegmentCount_);//更改回程数据的segments			
				this.flightData[i].rangeSegmentCount = leg;	//更改回程数据的rangeSegmentCount
				var segsCount = leg.split("-")[1];//获取回程的航段数，若为1段,则为直飞
				if(segsCount==1){
					this.flightData[i].directFlight = true;
				}
				//添加索引
				this.$set(this.flightData[i],'index',i); 
				this.$set(this.storeData[i],'index',i); 
			}
			this.asPrice = false;
			this.filterAsPrice();//默认价格从低到高排序
		},
		/*判断是否经停*/
		stopOver(flight){
			
			var directFlight = flight.directFlight;
			if(!directFlight) return {show:false};//优先展示中转
			var length = flight.segments[0].stopOver.length;
			if(length==0) return {show:false};
			if(length == 1){
				return {show:true,city:flight.segments[0].stopOver[0].stopAirport}
			}else if(length == 2){
				return {show:true,city:'2次',color:true}
			}else{
				return {show:true,city:'多次'}
			}	
		},
		/*判断是否中转*/
		directFlight(flight){
			var rangeSegmentCount = flight.rangeSegmentCount;
			var segmentCount = rangeSegmentCount.split(",")[0].split("-")[1];
			if(segmentCount == 1){
				return {show:false}
			}else if(segmentCount == 2){
				return {show:true,city:flight.segments[0].toCityCn}
			}else if(segmentCount == 3){
				return {show:true,city:'2次',color:true}
			}else{
				return {show:true,city:'多次'}
			}			
		}		
	},
	components:{
	  	Loading,
	},
	created() {		
		this.RECORD_INTERRT({type:'go'});
		document.title = "选回程 "+this.city.arrCity+"—"+this.city.depCity;	
		this.loading = false;
		this.getBackData();
	}
}